import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pipeline.LianJiaPipeline;
import processer.LianJiaProcesser;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Spider;
import util.LianJiaUtil;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LianJiaSpiderStarter {
    public static void main(String[] args) throws IOException, InterruptedException {

        Logger logger = LoggerFactory.getLogger(LianJiaSpiderStarter.class);
        String url ="https://app.api.lianjia.com/house/ershoufang/searchv5?city_id=440100&is_history=0&full_filters=0&limit_offset=0&has_recommend=0&is_suggestion=0&isFromMap=false&ad_recommend=1&limit_count=20";
        LianJiaProcesser processer = new LianJiaProcesser();
        LianJiaPipeline pipeline = new LianJiaPipeline();
        Request request = LianJiaUtil.createGetMethodRequest(url);
        Spider lianJiaSpider = Spider.create(processer).addPipeline(pipeline).addRequest(request);
        lianJiaSpider.thread(5);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        File file = new File(ClassLoader.getSystemClassLoader().getResource("").getPath() + "/time/time.json");
        String time = JSONObject.parseObject(FileUtils.readFileToString(file)).getString("time");
        logger.info("任务将会在{}执行",time);
        //每天凌晨两点执行一次
        while(true){
            Thread.sleep(1000);
            if(StringUtils.equals(time,simpleDateFormat.format(new Date()))){
                lianJiaSpider.run();
            }
        }
    }
}
